package com.wut.clock.controller;

import com.wut.clock.aop.log.BusinessType;
import com.wut.clock.aop.log.Log;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.dto.admin.MenuParams;
import com.wut.clock.service.IMenuService;
import com.wut.clock.vo.admin.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 菜单管理
 * @date 2022/5/30 17:18
 */
@Slf4j
@Api(value="菜单管理器",tags = {"菜单管理器"})
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/menu")
public class MenuController {

    private final IMenuService menuService;

    @ApiOperation("添加菜单")
    @PostMapping("/addOneMenu")
    @RequiresPermissions("sys:menu:add")
    @Log(title = "添加菜单",businessType = BusinessType.INSERT)
    public CommonResult addOneMenu(@Valid @RequestBody MenuParams menuParams){
        if(menuService.insertOneMenu(menuParams)>0){
            return CommonResult.success("添加菜单成功");
        }
        return CommonResult.failed("添加菜单失败");
    }

    @ApiOperation("删除菜单")
    @PostMapping("/deleteOneMenu/{menuId}")
    @RequiresPermissions("sys:menu:delete")
    @Log(title = "删除菜单",businessType = BusinessType.DELETE)
    public CommonResult deleteOneMenu(@NotNull @PathVariable Integer menuId){
        if(menuService.deleteOneMenu(menuId)>0){
            return CommonResult.success("删除菜单成功");
        }
        return CommonResult.failed("删除菜单失败");
    }

    @ApiOperation("更新菜单")
    @PostMapping("/updateOneMenu")
    @RequiresPermissions("sys:menu:update")
    @Log(title = "更新菜单",businessType = BusinessType.UPDATE)
    public CommonResult updateOneMenu(@Valid @RequestBody MenuParams menuParams){
        if(menuService.updateMenu(menuParams)>0){
            return CommonResult.success("修改菜单成功");
        }
        return CommonResult.failed("修改菜单失败");
    }

    @ApiOperation("获取所有菜单")
    @GetMapping("/getMenuList")
    @RequiresPermissions("sys:menu:query")
    public CommonResult getMenuList(){
        List<MenuVo> menuVoList = menuService.listMenuVos();
        if(CollectionUtils.isNotEmpty(menuVoList)){
            return CommonResult.success(menuVoList,"获取菜单成功");
        }
        return CommonResult.failed("获取菜单失败");
    }

}
